﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Guest_room
{
    public partial class roominfo : Form
    {
        public roominfo()
        {
            InitializeComponent();
        }
        //定义适配器对象
        SqlDataAdapter daroominfo;
        DataSet dr = new DataSet();
        public static string room_id,room_price;

        Font f1 = new Font("UTF-8", 10);
        void init()
        {
            DB.GetCn();
            string str = "select * from 客房基本信息表 where 客房状态='空闲'";
            daroominfo = new SqlDataAdapter(str, DB.cn);
            daroominfo.Fill(dr, "room_info");
            dataGridView1.Font = f1;
            DB.cn.Close();
        }
        void showall()  //显示函数
        {
            DataView dvroominfo = new DataView(dr.Tables["room_info"]);
            dataGridView1.DataSource = dvroominfo;
            //筛选条件
            dvroominfo.RowFilter = "档次编号='" + roin.grade_id + "'";
            //隐藏列
            dataGridView1.Columns["档次编号"].Visible = false;
            //字体居中
            dataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //列名居中
            dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //列宽自适应窗体，显示完整列名和数据
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }
        void showXz()//显示选择列
        {
            DataGridViewCheckBoxColumn acCode = new DataGridViewCheckBoxColumn();
            acCode.Name = "acCode";
            acCode.HeaderText = "选择";
            //显示在第一列
            dataGridView1.Columns.Insert(0, acCode);
            //宽度为40
            dataGridView1.Columns[0].Width = 50;
        }
        private void roominfo_Load(object sender, EventArgs e)//窗体加载函数
        {
            label2.Text = Login.username ?? Home.username;
            init();
            showall();
            showXz();
        }

        private void button1_Click(object sender, EventArgs e)//填写信息
        {
            bool anyRowChecked = false; // 是否有任何一行被勾选
            if (dataGridView1.Rows.Count > 0)
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)  //遍历所有行
                {
                    string _selectValue = dataGridView1.Rows[i].Cells["acCode"].EditedFormattedValue.ToString();// 获取选择框的值

                    if (_selectValue == "True") // 如果被勾选
                    {
                        anyRowChecked = true;   // 有行被勾选
                        room_id = dataGridView1.Rows[i].Cells["客房编号"].Value.ToString();  // 获取当前行的产品编号
                        room_price= dataGridView1.Rows[i].Cells["客房单价"].Value.ToString();
                        break; // 一旦发现有勾选，则退出循环
                    }
                }
            }
            else
            {
                MessageBox.Show("没有数据");
                return;
            }
            if (!anyRowChecked || room_id == null || room_id == "")
            {
                MessageBox.Show("请选择一行数据");
                return;
            }
            else
            {
                this.Dispose();
                roapfo ri = new roapfo();
                ri.ShowDialog();
            }
        }

        private void button2_Click(object sender, EventArgs e)//关闭
        {
            this.Dispose();
            roin roin = new roin();
            roin.ShowDialog();

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)//多选变单选
        {
            if (dataGridView1.Rows.Count > 0)   //判断是否有数据
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)  //遍历所有行
                {
                    DataGridViewCheckBoxCell ck = dataGridView1.Rows[i].Cells["acCode"] as DataGridViewCheckBoxCell;    //获取当前行的选择框
                    ck.Value = (i != e.RowIndex) ? false : true;
                }
            }
        }
    }
}
